<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

class ServiceList
{
    public $login = true; //是否需要登录才能访问该接口
    public $title = "预约服务列表ok";
    public $group = '物业服务';
    public $desc = "";

    public $input = [
        'page_num' => 'int;label=分页;comment=默认为1',
        'page_size' => 'int;label=分页条数;comment=默认为5',
        'service_type_id' => 'int;required;label=分类id',
    ];

    public $output = [
        'page_num' => 'label=当前页码;',
        'page_size' => 'label=每页条数;',
        'page_count' => 'label=总条数;',
        'page_total' => 'label=总页数;',
        'list' => [
            [
                'id' => 'label=id;',
                'infor_title' => 'label=信息标题;',
                'service_type_id' => 'label=所属分类id;',
                'sort' => 'label=排序值;',
                'logo_img' => 'label=logo图;',
                'content' => 'label=信息详情;',
                'create_time' => 'label=创建时间;',
                'read_count' => 'label=阅读量;',
                'collect_count' => 'label=收藏量;',
            ]
        ]
    ];

    public function run($param, $uid)
    {
        $page_num = $param['page_num'] ?: 1;
        $page_size = $param['page_size'] ?: 10;

        $list = M('service')
            ->where(['state' => 1, 'service_type_id' => $param['service_type_id']])
            ->order('sort desc')->page($page_num, $page_size)
            ->select();
        foreach($list as &$item){
            $item['read_count'] = 0;
            $item['collect_count'] = 0;
        }
        $count = M('service')->where(['state' => 1])->count();
        return [
            'list' => $list,
            'page_count' => $count,
            'page_total' => ceil($count / $page_size)
        ];
    }
}
